from communication.asyncClient import AsyncClient
from robot.comm.command import SplitArgCommand, CutCommand
from robot.comm.pluginBase import Session
from robot.comm.status import ChatStatus


def judge_chat(session: Session):
    if not ChatStatus[session.id].switch:
        return False
    if session.is_group() and ChatStatus[session.id].at_switch:
        return False
    return True


def judge_baike_words(words):
    if len(words) < 2:
        return False, ''
    if words[-2] == '是' and words[-1] in {'什么', '谁'}:
        return True, ''.join(words[:-2])
    if words[1] == '是' and words[0] in {'什么', '谁'}:
        return True, ''.join(words[2:])
    return False, ''


@SplitArgCommand('百科', ['请输入要查询的内容：'])
@CutCommand(judge_baike_words).trim_filter(before=judge_chat)
async def baike(session: Session, question: str):
    if not question.strip():
        session.skip()
    async with AsyncClient('baike') as client:
        res = await client.send(question)
        res += '\n————来自百度百科'
        await session.reply(res)
